#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"

#include <stdio.h>
#include <string.h>
#include "led.h"
#include "uart.h"

/**
 * @brief       程序入口
 * @param       无
 * @retval      无
 */
void app_main(void)
{
    esp_err_t ret;
    uint32_t len=0;
    uint32_t times=0;
    char datas[UART_BUFFER_SIZE] = {0};

    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ESP_ERROR_CHECK(nvs_flash_init());
    }

    led_init();
    uart0_init(115200);
    while (1)
    {
        uart_get_buffered_data_len(UART_UX, (size_t *)&len);
        if (len > 0)
        {
            uart_read_bytes(UART_UX, datas, len, 100);
            printf("\n");
            printf("len:%"PRIu32"\n",len);
            uart_write_bytes(UART_UX,datas,strlen(datas));
            memset(datas, 0, strlen(datas));
        }
        else
        {
            times++;
            if(times%3000==0)
            {
                printf("UART TEST\n");
            }
            if(times%1000==0)
            {
                printf("HVP\n");
            }
            if(times%100==0)
            {
                LED_TOGGLE();
            }
        }
        vTaskDelay(pdMS_TO_TICKS(10));
    }
}